<?php


class AttributeTable extends Doctrine_Table
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('Attribute');
    }

    /**
     *
     * @param string $dql
     * @param array $params
     * @param int $limit
     * @param <type> $hydrationMode
     * @return Doctrine_Collection or Array
     */
    public function findWithLimit($dql, $params = array(), $limit = 10, $hydrationMode = null)
    {
        return $this->createQuery('attr')
                ->where($dql, $params)
                ->limit($limit)
                ->execute();
    }

    /**
     * Override
     * @see parent::create($array)
     * @param array $params
     */
    public function create(array $params = array())
    {
        // check if this unit already exist
        $unit = Doctrine::getTable('Unit')->exists(array('unit' => $params['unit']));
        if (empty($unit))
        {
            $unit = new Unit('Unit', true);
            $unit->setUnit($params['unit']);
            
            $unit->save();
        }

        $params['unitid'] = $unit->id;

        return parent::create($params);
    }

    /**
     * Find a record by fieldvalues, or if not found return new Attribute object
     * @param array $params array of $params[fieldname] = fieldvalue
     * @return Attribute
     */
    public function findOrCreate(array $params = array())
    {
        $record;

        if (isset($params['id']))
            $record = $this->findOneBy('id', $params['id']);

        if (empty($record) && isset($params['attributename']))
            $record = $this->findOneBy('attributename', $params['attributename']);

        if (empty($record))
        {
            $record = $this->create($params);
            $record->save();
        }

        return $record;
    }

    public function insertIfNotExist($params = array()) {
        
        sfContext::getInstance()->getLogger()->debug('insertIfNotExist');

        if (!isset($params['attributename'])) {
            sfContext::getInstance()->getLogger()->debug('empty attributename');
            return false;
        }

        $record = $this->findOneBy('attributename', $params['attributename']);

        if ($record) {
            // if record found, return false
            sfContext::getInstance()->getLogger()->debug('attribute ' . $record->getAttributename() . ' already exists.');
            return false;
        } else {
            // else create new attribute
            sfContext::getInstance()->getLogger()->debug('create new attribute ' . $params['attributename']);
            $record = $this->create($params);
            $record->save();

            // insert into objectunit
            $obUnit = new Objectunit('Objectunit', true);
            $obUnit->setObjectid($record->getId())
                    ->setUnitid($record->getUnitid())
                    ->setObjecttyperefid(Types::$OBJECT_TYPE[Types::$OBJECT_TYPE_ATTRIBUTE])
                    ->save();

            return $record;
        }
    }

    public function selectBy($columns = array()) {
        $alias = 'attr';
        $cols = "";
        foreach($columns as $c) {
            $cols .= $alias . "." . $c . ",";
        }
        // remove trailing comma
        $cols = empty($cols) ? "" : substr($cols, 0, -1);
        return $this->createQuery($alias)
                ->select($cols)
                ->orderBy($alias.'.attributename asc')
                ->fetchArray();
    }
}